package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.UserAssistantDecoration;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AssistantDecorationDaoImpl extends TempDataObjectDao<UserAssistantDecoration> {
    public static String getAssistantDecorationSql = "select * from `t_u_assistant_decoration` where `userId`=?";

    public Map<Integer, UserAssistantDecoration> getAssistantDecoration(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserAssistantDecoration> decorationMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAssistantDecorationSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                decorationMap.put(rs.getInt("decorationId"), parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return decorationMap;
    }

    public static String updateAssistantDecorationSql = "update `t_u_assistant_decoration` set decorationLevel=?,dressScore=? where userId=? and decorationId=?";

    @Override
    protected boolean updateData(UserAssistantDecoration userAssistantDecoration) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantDecoration.getDecorationLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantDecoration.getDressScore()));
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantDecoration.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantDecoration.getDecorationId()));
        if (getDb().executeNoneQuery(updateAssistantDecorationSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAssistantDecorationSql = "insert into `t_u_assistant_decoration`(userId,decorationId,decorationLevel,dressScore) values (?,?,?,?)";

    @Override
    protected boolean addData(UserAssistantDecoration userAssistantDecoration) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantDecoration.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantDecoration.getDecorationId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantDecoration.getDecorationLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantDecoration.getDressScore()));
        if (getDb().executeNoneQuery(insertAssistantDecorationSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String getTopDecorationsSql = "select * from `t_u_assistant_decoration` where `userId`=? order by `dressScore` DESC";

    public List<UserAssistantDecoration> getTopDecoration(long userId, int topNum) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserAssistantDecoration> decorationList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTopDecorationsSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next() && decorationList.size() < topNum) {
                decorationList.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return decorationList;
    }

    @Override
    protected UserAssistantDecoration parseData(ResultSet rs) throws SQLException {
        UserAssistantDecoration userAssistantDecoration = new UserAssistantDecoration();
        userAssistantDecoration.setUserId(rs.getLong("userId"));
        userAssistantDecoration.setDecorationId(rs.getInt("decorationId"));
        userAssistantDecoration.setDecorationLevel(rs.getInt("decorationLevel"));
        userAssistantDecoration.setDressScore(rs.getLong("dressScore"));

        return userAssistantDecoration;
    }


}
